package pokerclient.core;

import common.GameAction;
import common.TrunkatedPlayer;

/**
 * A Utilities class. Containing mostly(only?) static methods.
 */
public class Utils {
	private static final String[] numerators = { "azeroth", "first", "second",
			"third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth",
			"tenth", "eleventh", "twelfth" };
	private static final String[] gameActionNames = { "Went All-in", "Betted", "Called",
			"Checked", "Connected", "Disconnected", "Folded", "Won", "Lost", "Is out (game-over)" };
	private static final GameAction[] gameActions = { GameAction.ALL_IN,
			GameAction.BET, GameAction.CALL, GameAction.CHECK,
			GameAction.CONNECT, GameAction.DISCONNECT, GameAction.FOLD, GameAction.WIN, GameAction.LOSE, GameAction.GAME_OVER};


	/**
	 * Takes in a number between one and twelve and returns a String with some
	 * grammar.
	 * 
	 * @param i
	 *            a number
	 * @return some grammar
	 */
	public static String intNumeration(int i) {
		if (i > 0 && i < 13) {
			return numerators[i];
		} else {
			return null;
		}
	}

	/**
	 * Takes in a list of TrunkatedPlayers and returns a list of Players of the
	 * same size. It does, however, leave out the main player completely.
	 * 
	 * @param trunkPlayerList
	 *            the incoming List<TrunkatedPlayer>
	 * 
	 * @return a List<Player>
	 */
	public static Player[] playerListConversion(
			TrunkatedPlayer[] trunkPlayerList, int mainPlayerPosition) {
		Player[] playerList = new Player[4];
		for (int i = 0; i < 4; i++) {
			if (trunkPlayerList[i] == null || i == mainPlayerPosition) {
				playerList[i] = null;
			} else {
				playerList[i] = new Player(trunkPlayerList[i]);
			}
		}
		return playerList;
	}

	/**
	 * Returns the number of non-null elements in a given array.
	 */
	public static int elementsInArray(Object[] elements) {
		int counter = 0;
		for (int i = 0; i < elements.length; i++) {
			if (elements[i] != null) {
				counter++;
			}
		}
		return counter;

	}

	/**
	 * 
	 * @param action
	 * @return
	 */
	public static String getGameActionText(GameAction action) {
		for (int i = 0; i < gameActions.length; i++) {
			if (gameActions[i] == action) {
				return gameActionNames[i];
			}
		}
		return null;
	}
}
